package com.example.springai.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class ToolCallHandler {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String handleToolCall(String functionName, String arguments) {
        try {
            if ("get_weather".equals(functionName)) {
                // 解析参数
                Map<String, String> parameters = objectMapper.readValue(arguments, new TypeReference<Map<String, String>>() {});
                String location = parameters.get("location");
                return getWeather(location);
            } else {
                return "Unsupported tool call.";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error handling tool call.";
        }
    }

    private static String getWeather(String location) {
        // 模拟天气查询
        return "The weather in " + location + " is 35 °C.";
    }
}
